#include <iostream>
#include <utility>

class CTemp {
 public:
  CTemp() { std::cout << "CTemp: 构造" << std::endl; }
  CTemp(CTemp& other) { std::cout << "CTemp: 拷贝构造" << std::endl; }
  CTemp(CTemp&& other) { std::cout << "CTemp: 移动构造" << std::endl; }
  ~CTemp() { std::cout << "CTemp::~CTemp()" << std::endl; }

  int sum;
};

CTemp foo2() ;
// 2.注意实现：返回时优化
CTemp foo() {
  CTemp x;
  
  return x;  // 理论上作用域在该函数中就结束了, 但此处编译器有做优化:
             // Clang/GCC中, CTemp 对象没有被析构
             // VC++ 中，此处调用了移动构造，相当于 return std::move(x);
}

CTemp foo2() {
  CTemp x;
  for (int i = 0; i < 100; i++) {
    x.sum += i + 1;
  }
  std::cout << "call foo2()" << std::endl;
  return x;  //作用域在该函数中就结束了
}

CTemp foo_move() {
  CTemp x;
  return std::move(x);  //作用域在该函数中就结束了
}

void copy_ctemp(const CTemp& temp) {
  std::cout << "copy_ctemp(const CTemp&)" << std::endl;
}

int main(int argc, char* argv[]) {
  auto a = foo();
  std::cout << "-----------" << std::endl;
  auto a2 = foo2();
  std::cout << "-----------" << std::endl;
  auto b = foo_move();

  std::cout << "---------------------------" << std::endl;
  copy_ctemp(CTemp());

  std::cout << "-----------End !-----------------" << std::endl;
  return 0;
}